package LeetCode;

/**
 * @Author:galaxy_yan
 * @Date: 2022/10/7
 * @Time: 16:04
 * @Description
 * https://leetcode.cn/problems/remove-duplicates-from-sorted-array/
 * 快慢指针思想
 */
public class RemoveDuplicates {
        public static int removeDuplicates(int[] nums) {
//                slow前均为不重复的元素
                int slow=0;
//                fast 如果与fast-1 不相同 则需要放在slow位置
                for (int fast = 1; fast < nums.length ; fast++) {
                        if(nums[fast]!=nums[slow]){
                                int tmp=nums[fast];
                                nums[fast]=nums[slow+1];
                                nums[slow+1]=tmp;
                                slow++;
                        }
                }
                return slow+1;
        }

        public static void main(String[] args) {
                int[] nums = {0,0,1,1,1,2,2,3,3,4};
                System.out.println(removeDuplicates(nums));
                for (int i:
                        nums) {
                        System.out.println(i);
                }
        }
}
